package com.coocaa.x.service.litecontent.providers.setting.host;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.x.service.litecontent.LiteContentProvider;
import com.coocaa.x.service.litecontent.LiteContentProvider.LiteContentProviderHost;
import com.coocaa.x.service.litecontent.LiteContentProvider.ResourceHandler;
import com.coocaa.x.service.litecontent.data.ResultPackage;
import com.coocaa.x.service.litecontent.data.setting.com_coocaa_app.RP_Startup_App;
import com.coocaa.x.service.litecontent.data.setting.com_coocaa_app.RP_Startup_App.Startup_App_Item;
import com.coocaa.x.service.litecontent.tools.KVCache;

public class SettingHost_Com_Coocaa_App extends LiteContentProviderHost {
    private static SettingHost_Com_Coocaa_App instance = null;

    public static synchronized void create(Context context, LiteContentProvider provider) {
        if (instance == null)
            instance = new SettingHost_Com_Coocaa_App(context, provider);
    }

    private static final String MY_HOST = "com.coocaa.app";

    public SettingHost_Com_Coocaa_App(Context context, LiteContentProvider provider) {
        super(context, provider);
    }

    @Override
    public String myHost() {
        return MY_HOST;
    }

    @SuppressWarnings("unused")
    // 2.2.1、開機啓動應用
    private ResourceHandler startup_app = new ResourceHandler(this, "/startup_app") {
        @Override
        public ResultPackage call(Uri uri) {
            String key = "value";
            PackageManager pm = mContext.getPackageManager();
            KVCache<String, String> kvCache = getKVCache();
            RP_Startup_App rp = new RP_Startup_App();
            String action = uri.getQueryParameter("action");
            if (action.equals("get")) {
                String value = null;
                if (kvCache.contains(key))
                    value = kvCache.get(key);
                if (value != null) {
                    JSONObject obj = JSONObject.parseObject(value);
                    String pkgname = obj.getString("pkgname");
                    String activity = obj.getString("activity");
                    if (!needFilterAutoStart(pkgname, activity)) {
                        String label = activity;
                        try {
                            ActivityInfo ai = pm.getActivityInfo(new ComponentName(pkgname, activity),
                                    0);
                            label = ai.loadLabel(pm).toString();
                            Startup_App_Item item = new Startup_App_Item(activity, label, pkgname);// MyResourceItem.parseJObject(value,
                            rp.resource.add(item);
                        } catch (NameNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else if (action.equals("set")) {
                String pkgname = uri.getQueryParameter("pkgname");
                String activity = uri.getQueryParameter("activity");
                if (pkgname.equals("") || activity.equals(""))
                    kvCache.remove(key);
                else
                    kvCache.save(key, new Startup_App_Item(activity, activity, pkgname).toString());
            }
            return rp;
        }

        private boolean needFilterAutoStart(String pkg, String activity) {
            int settingCode = 0;
            try {
                settingCode = mContext.getPackageManager().getPackageInfo("com.tianci.system", 0).versionCode;
            } catch (PackageManager.NameNotFoundException e) {
                settingCode = 0;
                e.printStackTrace();
            }

            if (settingCode >= 21) {
                if ((pkg.equals("com.coocaa.homeshell")
                        || pkg.equals("com.tianci.verysamplehome")
                        || pkg.equals("com.skyworth.simplehomepage")
                      //  || (pkg.equals("com.tianci.appstore") && activity.equals("com.coocaa.launcher.EasyLauncherActivity"))
                )) {
                    return true;
                }
            }
            return false;
        }
    };
}
